(function (window,angular) {
	'use strict';
	var app=angular.module('todoApp',['todoApp.service','todoApp.directive']);
	app.controller('todoCtrl', function ($scope,storageService,$filter) {

		 $scope.todoList=storageService.getTodo();
		//增加
		$scope.todoText="";
		$scope.addTodo= function () {
			if($scope.todoText.length>0){
				storageService.addTodo($scope.todoText);
				$scope.todoText="";
			}

		}
		//删除
		$scope.delTodo= function (todo) {

			storageService.delTodo(todo);

		}

		//默认修改状态是没有
		$scope.editingID=-1
		$scope.editTodo= function (id) {
			//双击的时候传入id,赋值
			$scope.editingID=id;

		}
		//失去焦点的时候
		$scope.editInit= function (todo) {
			//如果长度小0,删除这个数据,否则就保存
			if(todo.text.length<=0){
				$scope.delTodo(todo)
			}else{
				storageService.editTodo();
			}

			//同时把修改状态改变下
			$scope.editingID=-1
		}
		//子功能

		//过滤未完成数量
		$scope.$watch('todoList', function (newVal) {
			//统计已完成数据
			$scope.todoCount=$filter('filter')(newVal,{isCompleted:false}).length;
			//判断有没有数据,如果没有隐藏
			$scope.isShowClearCompleted=$filter('filter')(newVal,{isCompleted:false}).length>0?true:false;

			$scope.toggleAll=!$scope.todoCount;
			
		},true)
		//清除已完成的数据
		$scope.clearCompleted= function () {
			$scope.todoList=storageService.clearCompleted();
			
		}
		//未点击的时候数据不确定
		$scope.todoListFilter={};

		$scope.changeFilter= function (digit) {
			switch (digit){
				case 1:
					$scope.todoListFilter={isCompleted:false};
					break;
				case 2:
					$scope.todoListFilter={isCompleted:true};
					break;
				default:
					$scope.todoListFilter={};
			}
		}

		$scope.changeAll= function () {
			$scope.todoList=storageService.changeAll($scope.toggleAll)
		}
	})

	
})(window,angular);
